package com.cyzy.controller;


import com.cyzy.dto.Result;
import com.cyzy.pojo.TrainOrder;
import com.cyzy.pojo.UserOrder;
import com.cyzy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    //查找订单
    @GetMapping("/select")
    public Result getOrderList(long userId) {
        List<UserOrder> order = orderService.selectOrder(userId);
        if (order != null) {
            return Result.ok().message("查询成功").data("data", order);
        }
        return Result.error().message("查询失败");
    }

   //取消订单
    @PutMapping("/cancel")
    public Result cancelOrder(@RequestBody TrainOrder trainOrder) {
        int i = orderService.cancelOrder(trainOrder);
        if (i > 0) {
            return Result.ok().message("取消成功");
        }
        return Result.error().message("取消失败");
    }

    //删除订单
    @DeleteMapping("/delete")
    public Result deleteOrder( TrainOrder trainOrder) {
        System.out.println(trainOrder.getOrderId()+"============");
        int i = orderService.deleteOrder(trainOrder);
        if (i > 0) {
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");
    }

    //查询当前订单信息
    @GetMapping("/getOrderInfo")
    public Result getOrderInfo( long orderId) {
        TrainOrder trainOrder = orderService.selectOrderById(orderId);
        if (trainOrder != null) {
            return Result.ok().message("查询成功").data("data", trainOrder);
        }
        return Result.error().message("查询失败");
    }

    //余额支付
    @PutMapping("/payBalance")
    public Result payBalance(@RequestBody TrainOrder trainOrder) {
        int i = orderService.payBalance(trainOrder);
        if (i > 0) {
            return Result.ok().message("支付成功");
        }
        return Result.error().message("余额不足使用支付宝支付");
    }
}
